﻿var blockPrefabs:GameObject[] = new GameObject[2]; //Lets say you have only 2 different blocks.
var lastBlock:GameObject; // Store the last/current block. Remember that the first block shoul be in scene and assigned to this variable.
 
private var nextStop:Vector3;
 
function Start(){
    nextStop = transform.position + Vector3.forward *0.1;
}
 
function Update () {
    RollCamera();
    var currPos:Vector3 =transform.position;
    print(Vector3.Distance(nextStop,currPos));
    if(Vector3.Distance(nextStop,currPos)>5){
        CreateNewBlock();
        nextStop = transform.position + Vector3.forward *0.1;
    }
}
 
function CreateNewBlock(){
    var insPos:Vector3 = lastBlock.transform.Find("Front_Anchore").transform.position;
    var newBlock:GameObject = Instantiate(blockPrefabs[Mathf.Floor(Random.Range(0,2))],insPos, lastBlock.transform.rotation);
    lastBlock = newBlock;
}
 
function RollCamera(){
    transform.Translate(Input.GetAxis("Horizontal") * Vector3.right * 0.2);
}